package com.just.just_bankreputationsystem.Controller;

import com.just.just_bankreputationsystem.Model.Type;
import com.just.just_bankreputationsystem.Service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("Type")
public class TypeController {
    @Autowired
    private TypeService typeService;


    @GetMapping("/list")
    public String showAllBank(Model model){
        List<Type> types = typeService.selectAlltype();
        model.addAttribute("types",types);
        return "type/typeList";
    }

    @GetMapping("toAdd")
    public String add(){
        return "type/typeAdd";
    }

    @PostMapping("/add")
    public String typeAdd(Type type){
        int ref = typeService.typeAdd(type);
        if(ref>0){
            return "redirect:list";//重定向
        }else{
            return "type/typeAdd";
        }
    }

    @GetMapping("del")
    public String delTypeById(int id){
        typeService.delTypeById(id);
        return "redirect:list";
    }

    @GetMapping("edit")
    public String toEdit(int id,Model model){
        Type type= typeService.selectTypeById(id);
        model.addAttribute("type",type);
        return "type/typeEdit";
    }

    @PostMapping("edit")
    public String doEdit(Type type){
//        System.out.println(bank.getId());
//        System.out.println(bank.getName());
//        System.out.println(bank.getProfit());
//        System.out.println(bank.getProfitRate());
//        System.out.println(bank.getReputation());
//        System.out.println(bank.getAppointment());
        int ref = typeService.updateType(type);
        if(ref>0){
            return "redirect:list";
        }else{
            return "type/typeEdit";
        }
    }
}
